package api

import (
	"gateway/basic/config"
	user "gateway/basic/proto/userproto"
	"gateway/handler/request"
	"gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// GetFinancialStats 获取财务统计数据
// @Summary 获取财务统计数据
// @Description 根据时间范围获取财务统计数据
// @Tags 财务统计
// @Accept json
// @Produce json
// @Param request query request.GetFinancialStatsRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse{data=request.FinancialStats}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/finance/stats [get]
func GetFinancialStats(c *gin.Context) {
	var req request.GetFinancialStatsRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.SrvError(c, "参数格式错误", err.Error())
		return
	}

	// 调用RPC服务获取财务统计数据
	stats, err := config.UserClient.GetFinancialStats(c, &user.GetFinancialStatsReq{
		StartDate: req.StartDate,
		EndDate:   req.EndDate,
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}

	response.Success(c, "财务统计数据获取成功", stats)
}

// GetFinancialSummary 获取财务汇总信息
// @Summary 获取财务汇总信息
// @Description 获取指定日期的财务汇总信息
// @Tags 财务统计
// @Accept json
// @Produce json
// @Param request query request.GetFinancialSummaryRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse{data=request.FinancialSummary}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/finance/summary [get]
func GetFinancialSummary(c *gin.Context) {
	var req request.GetFinancialSummaryRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}

	// 调用RPC服务获取财务汇总信息
	summary, err := config.UserClient.GetFinancialSummary(c, &user.GetFinancialSummaryReq{
		StartDate: req.StartDate,
		EndDate:   req.EndDate,
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "财务汇总信息获取成功", summary)
}

// GetIncomeBreakdown 获取收入明细
// @Summary 获取收入明细
// @Description 根据时间范围获取收入明细
// @Tags 财务统计
// @Accept json
// @Produce json
// @Param request query request.GetIncomeBreakdownRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse{data=[]request.IncomeBreakdown}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/finance/income_breakdown [get]
func GetIncomeBreakdown(c *gin.Context) {
	var req request.GetIncomeBreakdownRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.SrvError(c, "参数格式错误", err.Error())
		return
	}
	// 调用RPC服务获取收入明细
	incomeBreakdown, err := config.UserClient.GetIncomeBreakdown(c, &user.GetIncomeBreakdownReq{
		StartDate: req.StartDate,
		EndDate:   req.EndDate,
		Page:      int32(req.Page),
		PageSize:  int32(req.PageSize),
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "收入明细获取成功", incomeBreakdown)
}

// GetExpenseBreakdown 获取支出明细
// @Summary 获取支出明细
// @Description 根据时间范围获取支出明细
// @Tags 财务统计
// @Accept json
// @Produce json
// @Param request query request.GetExpenseBreakdownRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse{data=[]request.ExpenseBreakdown}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/finance/expense_breakdown [get]
func GetExpenseBreakdown(c *gin.Context) {
	var req request.GetExpenseBreakdownRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "参数格式错误", err.Error())
		return
	}
	// 调用RPC服务获取支出明细
	expenseBreakdown, err := config.UserClient.GetExpenseBreakdown(c, &user.GetExpenseBreakdownReq{
		StartDate: req.StartDate,
		EndDate:   req.EndDate,
		Page:      int32(req.Page),
		PageSize:  int32(req.PageSize),
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "支出明细获取成功", expenseBreakdown)
}

// GetTransactionList 获取交易明细列表
// @Summary 获取交易明细列表
// @Description 根据条件获取交易明细列表
// @Tags 财务统计
// @Accept json
// @Produce json
// @Param request query request.GetTransactionListRequest true "请求参数"
// @Success 200 {object} response.SuccessResponse{data=[]request.TransactionItem}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /api/finance/transactions [get]
func GetTransactionList(c *gin.Context) {
	var req request.GetTransactionListRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.SrvError(c, "参数格式错误", err.Error())
		return
	}
	// 调用RPC服务获取交易明细列表
	transactionList, err := config.UserClient.GetTransactionList(c, &user.GetTransactionListReq{
		StartDate:       req.StartDate,
		EndDate:         req.EndDate,
		TransactionType: req.Type,
	})
	if err != nil {
		response.SrvError(c, "RPC服务调用失败", err.Error())
		return
	}
	response.Success(c, "交易明细列表获取成功", transactionList)
}
